package com.icloud.cer.rest.msg.processor;

import java.net.InetSocketAddress;
import java.net.SocketAddress;

import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.icloud.cer.rest.Constants;
import com.icloud.cer.rest.msg.MessageHeader;
import com.icloud.cer.rest.msg.MessageHolder;
import com.icloud.cer.rest.msg.MsgConstants;
import com.icloud.cer.rest.msg.bean.RegistInfo;
import com.icloud.cer.rest.msg.bean.RegistResponse;
import com.icloud.cer.rest.util.MessageUtils;

public class Msg200Processor extends BaseMessageProcessor implements MessageProcessor{
	private final Logger log = LoggerFactory.getLogger(Msg200Processor.class);
	/**
	 * 处理客户端注册消息
	 */
	@Override
	public void process(IoSession session, MessageHolder holder) {
		RegistInfo regInfo = (RegistInfo)holder.getValue();
		SocketAddress socketAddr = session.getRemoteAddress();
		if(socketAddr instanceof InetSocketAddress) {
			//log.info("add client to map." + socketAddr);
			connectorManager.addClient(session.getId(),regInfo.getEncryptorIdStr(),regInfo.getEncryptorNameStr(), (InetSocketAddress)socketAddr);
		} else {
			log.error("not instanceof InetSocketAddress");
			return;
		}
		MessageHeader header = MessageUtils.getHeader(Constants.SEQUENCE_LENGTH, MsgConstants.SERVER_RES_REG);
		header.setSize_(RegistResponse.BYTE_COUNT);
		
		RegistResponse reg = new RegistResponse();
		//reg.setSequence(holder.getSequence());
		reg.setLoginRet(0);//注册成功
		
		MessageHolder resHolder = new MessageHolder(MsgConstants.SERVER_RES_REG);
		//resHolder.setSequence(holder.getSequence());
		resHolder.setHead(header);
		resHolder.setValue(reg);
		session.write(resHolder);
		long sessionId = session.getId();
		connectorManager.addSession(sessionId, session);
		
	}

}
